package util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

public class DBUtil {
    private static final  Logger logger = LoggerFactory.getLogger(DBUtil.class);
    public static Connection connection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection
                ("jdbc:mysql://8.131.75.42:3306/czbk_member?characterEncoding=UTF-8&useSSL=false", "root", "TestDev01_p2p");
    }

    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Statement st) {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet re) {
        if (re != null) {
            try {
                re.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, Statement st, ResultSet rs) {
        close(rs);
        close(st);
        close(conn);
    }

    public static Map<String, Object> get_one(String sql)  {
        Map<String, Object> data = new HashMap<>();
        logger.info("我是"+"sql={}",sql);
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
        try {
            connection = connection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                ResultSetMetaData metaData = resultSet.getMetaData();
                System.out.println("我是meta" + metaData);
                for (int i=1; i <=metaData.getColumnCount(); i++) {
                    //获取列的名称
                    String columnLabel = metaData.getColumnLabel(i);
                    Object resultSetObject = resultSet.getObject(i);
                    System.out.println("列的名称" + columnLabel);
                    System.out.println("值" + resultSetObject);
                    data.put(columnLabel, resultSetObject);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }finally {
            close(connection,preparedStatement, resultSet);
        }
        return data;
    }
    public static void execute(String sql) throws SQLException, ClassNotFoundException {
        logger.info("sql={}",sql);
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try{
            connection=connection();
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.execute();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("e:",e);
        }finally {
            close(preparedStatement);
            close(connection);
        }

    }

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
//        String sql="select `read`,title,pub_date,id,comment from t_book where id=1";
//        Map<String, Object> one = get_one(sql);
//        System.out.println(one);
        String sql="delete fro m t_book where id=1";
        execute(sql);
    }
}

